﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using YXShop.IDAL.SystemInfo;

namespace YXShop.SQLServerDAL.SystemInfo
{
    public class ShopStyle :DbBase,IShopStyle
    {
        #region "DataBase Operation"
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YXShop.Model.SystemInfo.ShopStyle model)
        {
            SqlParameter[] paras = (SqlParameter[])this.ValueParas(model);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + Pre + "shopstyle(");
            strSql.Append("[Name], [SampleMapPath], [IsDefault], [Version], [CrateTime], [TemplatePath])");
            strSql.Append(" values (");
            strSql.Append("@Name, @SampleMapPath, @IsDefault, @Version, @CrateTime, @TemplatePath)");
            strSql.Append(";select @@IDENTITY");
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(), paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新任意一个字段
        /// </summary>
        /// <param name="id"></param>
        /// <param name="columnName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Amend(int id, string columnName, object value)
        {
            string sequel = "Update " + Pre + "shopstyle set ";
            sequel = sequel + "[" + columnName + "] =@Value ";
            sequel = sequel + " Where [id] = @id"; ;
            SqlParameter[] paras = new SqlParameter[] {
                new SqlParameter("@Value", value),
                new SqlParameter("@id", id) 
            };
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(sequel, paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        #endregion

        #region "DataB Load"
        /// <summary>
        /// 得到一个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public YXShop.Model.SystemInfo.ShopStyle GetModelByID(int id)
        {
            YXShop.Model.SystemInfo.ShopStyle model = new YXShop.Model.SystemInfo.ShopStyle();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 [id], [Name], [SampleMapPath], [IsDefault], [Version], [CrateTime] from " + Pre + "shopstyle");
            strSql.Append(" where [id]=@id ");
            SqlParameter[] parameters = (SqlParameter[])this.ValueIDPara(id);
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                if (reader.Read())
                {
                    model.ID = reader.GetInt32(0);
                    model.Name = reader.GetString(1);
                    model.SampleMapPath = reader.GetString(2);
                    model.IsDefault = reader.GetInt32(3);
                    model.Version = reader.GetString(4);
                    model.CreateTime = reader.GetDateTime(5);
                }
            }
            return model;
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <remarks></remarks>
        public ChangeHope.DataBase.DataByPage GetList()
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] " + Pre + "shopstyle [where] 1=1 [order by] CrateTime asc";
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        public List<YXShop.Model.SystemInfo.ShopStyle> GetColList()
        {
            List<YXShop.Model.SystemInfo.ShopStyle> list = new List<YXShop.Model.SystemInfo.ShopStyle>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  [id], [Name], [SampleMapPath], [IsDefault], [Version], [CrateTime] from  " + Pre + "shopstyle");
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.SystemInfo.ShopStyle model = new YXShop.Model.SystemInfo.ShopStyle();
                    model.ID = reader.GetInt32(0);
                    model.Name = reader.GetString(1);
                    model.SampleMapPath = reader.GetString(2);
                    model.IsDefault = reader.GetInt32(3);
                    model.Version = reader.GetString(4);
                    model.CreateTime = reader.GetDateTime(5);
                    list.Add(model);
                }
            }
            return list;
        }
        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        protected IDbDataParameter[] ValueParas(YXShop.Model.SystemInfo.ShopStyle model)
        {
            SqlParameter[] paras = new SqlParameter[7];
            paras[0] = new SqlParameter("@id", SqlDbType.Int, 4);
            paras[0].Value = model.ID;
            paras[1] = new SqlParameter("@Name", SqlDbType.VarChar, 50);
            paras[1].Value = model.Name;
            paras[2] = new SqlParameter("@SampleMapPath", SqlDbType.VarChar, 100);
            paras[2].Value = model.SampleMapPath;
            paras[3] = new SqlParameter("@IsDefault", SqlDbType.Int, 4);
            paras[3].Value = model.IsDefault;
            paras[4] = new SqlParameter("@Version", SqlDbType.VarChar,200);
            paras[4].Value = model.Version;
            paras[5] = new SqlParameter("@CrateTime", SqlDbType.DateTime, 8);
            paras[5].Value = model.CreateTime;
            paras[6] = new SqlParameter("@TemplatePath",SqlDbType.VarChar,200);
            paras[6].Value = model.TemplatePath;
            return paras;
        }
        /// <summary>
        /// 主键参数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IDbDataParameter[] ValueIDPara(int id)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@id", id);
            paras[0].DbType = DbType.Int32;
            return paras;
        }
        #endregion
    }
}
